<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证对象键值</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: #fff;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 400px;
            box-sizing: border-box;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            color: #666;
            margin-bottom: 10px;
            display: block;
        }

        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
        }

        .valid {
            color: #4CAF50;
        }

        .invalid {
            color: #e74c3c;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>验证对象键值</h1>
        <label for="inputObject">请输入对象 (例如：{"name": "小明", "age": 25})：</label>
        <input type="text" id="inputObject" placeholder='{"name": "小明", "age": 25}'>

        <label for="inputKeys">请输入允许的键 (用逗号分隔)：</label>
        <input type="text" id="inputKeys" placeholder="name, age">

        <button onclick="validateKeys()">验证</button>

        <div id="result" class="result"></div>
    </div>

    <script>
        const assertValidKeys = (obj, keys) =>
            Object.keys(obj).every(key => keys.includes(key));

        function validateKeys() {
            const objectInput = document.getElementById('inputObject').value;
            const keysInput = document.getElementById('inputKeys').value;

            try {
                const obj = JSON.parse(objectInput);
                const keys = keysInput.split(',').map(key => key.trim());
                const isValid = assertValidKeys(obj, keys);

                const resultDiv = document.getElementById('result');
                if (isValid) {
                    resultDiv.innerHTML = '<span class="valid">验证成功！所有键都是允许的。</span>';
                } else {
                    resultDiv.innerHTML = '<span class="invalid">验证失败！存在不允许的键。</span>';
                }
            } catch (error) {
                const resultDiv = document.getElementById('result');
                resultDiv.innerHTML = '<span class="invalid">输入的对象格式不正确，请检查。</span>';
            }
        }
    </script>

</body>

</html>